<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/10/12
  Time: 10:35
  To change this template use File | Settings | File Templates.
--%>
<%@page import="cn.darkfire.web.POJO.User"%>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    HashMap<String,User> hashMap = (HashMap<String, User>)application.getAttribute("hashMap");
    request.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");
    String age = request.getParameter("age");
    int age_N = 0;
    String [] hobbys = request.getParameterValues("hobby");
    try{
        age_N = Integer.parseInt(age);
        if(age_N<=0||age_N>=120){
            session.setAttribute("error","年龄输入错误");
            response.sendRedirect("error.jsp");
            return;
        }
    }catch (Exception e){
        out.print("请输入整数年龄");
        session.setAttribute("error","年龄输入错误");
        response.sendRedirect("error.jsp");
        return;
    }
     if(name==null||name.equals("")){
        session.setAttribute("error","用户名错误");
        out.print("姓名输入错误");
        response.sendRedirect("error.jsp");
    }else if(hobbys==null||hobbys.length<1){
        session.setAttribute("error","爱好输入错误");
        response.sendRedirect("error.jsp");
    }else{
        if(hashMap.containsKey(name)){
            session.setAttribute("error","该用户已经存在！");
            response.sendRedirect("error.jsp");
        }else{
            hashMap.put(name,new User(name,Integer.valueOf(age),hobbys)); //存入hashMap
            out.print("用户："+name+",入库成功！");
            session.setAttribute("user",new User(name,Integer.valueOf(age),hobbys));
            response.sendRedirect("show.jsp");
        }
    }
%>
<html>
<head>
    <title>验证</title>
</head>
<body>
</body>
</html>
